package org.wmine.core;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;



/**
 * @author Prafulla
 */
public class DatabaseManager {

	private ArrayList<String> tablesNameList;
	private Map<String,ArrayList<String>> tableColumnInformation;
	private boolean connected = false;

	private Connection con;
	private ResultSet rs,rs1;

	private static DatabaseManager databaseManager;
	private static String dsnName,user, password;	
	/**
	 *  
	 */
	private DatabaseManager() {
		connectAndFillDataStructures();
	}
public Connection getConnection(){
	return con;

}
	protected void connectAndFillDataStructures() {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con=DriverManager.getConnection(dsnName,user,password);
			connected = true;
			System.out.println("Connected");
		}
		catch(Exception e)	{
			System.out.println(e.getMessage());
			e.printStackTrace();

		}

		tablesNameList = new ArrayList<String>();
		tableColumnInformation = new TreeMap<String,ArrayList<String>>();
		fillTableList();
		Iterator<String> tableIterator = tablesNameList.iterator();
		while(tableIterator.hasNext())  {
			String tableName = tableIterator.next();
			fillTableInformation(tableName);
		}
	}

	/**
	 * Returns true if the database manager is connected to any database
	 * 
	 */
	public boolean isConnected() {
		return connected;
	}

	public Object[] getTableArray() {
		return tablesNameList.toArray();
	}

	public static void setDatabaseData(String pDsnName,String pUser,String pPassword) {
		dsnName = pDsnName;
		user = pUser;
		password = pPassword;
		//Because this method can be used to change the DSN,we need to connect 
		//to the database again  
		databaseManager = getInstance();
		databaseManager.connectAndFillDataStructures();
	}

	public static DatabaseManager getInstance() {
		if(databaseManager == null)
			databaseManager = new DatabaseManager();
		return databaseManager;

	}

	protected void fillTableList() {
		try 	{
			DatabaseMetaData metadata=con.getMetaData();
			
			rs=metadata.getTables(null,null,null,null);
			
			while(rs.next())
			{
				String tableName = rs.getString("TABLE_NAME");
				String tableType = rs.getString("TABLE_TYPE");
				if(tableType.equals("TABLE"))
					tablesNameList.add(tableName);
			}

		}
		catch(SQLException e)
		{

		}
	}

	protected void fillTableInformation(String tableName) {
		try
		{
			DatabaseMetaData metadata1=con.getMetaData();
			rs1=metadata1.getColumns(null,null,tableName,null);
			ArrayList<String> columnList = new ArrayList<String>();
			while(rs1.next()) {
				String columnName = rs1.getString("COLUMN_NAME");
				columnList.add(columnName);
			}
			tableColumnInformation.put(tableName,columnList);
		}
		catch(SQLException e) {

		}
	}

	/**
	 *  
	 * @return Returns the table name in the form of the Iterator
	 */
	Iterator<String> getAllTables() {
		return tablesNameList.iterator();
	}

	/**
	 * @param tableName The table whose information will be returned
	 * @return Returns the iterator which can be used to iterate thru' the columns of the particular table
	 */
	public Iterator<String> getTableInformation(String tableName) {
		return tableColumnInformation.get(tableName).iterator();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		guiDatabase db= new guiDatabase();

		Iterator<String> tableIterator = databaseManager.getAllTables();
		System.out.println("System contains following table(s)");
		while(tableIterator.hasNext()) {
			//Print the name of the table
			System.out.println(tableIterator.next());
		}		

		System.out.println("Now showing the table information for each table");
		Iterator<String> tableIterator1 = databaseManager.getAllTables();
		while(tableIterator1.hasNext()) 
		{
			String tableName = tableIterator1.next();
			System.out.println("Table "+tableName+ " has following columns" );
			Iterator<String> columnIterator = databaseManager.getTableInformation(tableName);
			while(columnIterator.hasNext())
				System.out.println(columnIterator.next());
			System.out.println();

		}
	}
}



class guiDatabase extends JFrame implements ActionListener
{

	JLabel lblConStr,lblUsername,lblPass,lblFilename;
	JButton btnConnect,btnTest,btnCancel;
	JTextField txtConStr,txtUsername,txtPass,txtFilename;
	public guiDatabase()
	{
		lblConStr=new JLabel("Connection String :");
		lblUsername=new JLabel("Username :");
		lblPass=new JLabel("Password :");
		lblFilename=new JLabel("Filename :");

		lblConStr.setBounds(10,10,150,50);
		lblUsername.setBounds(10,60,150,50);
		lblPass.setBounds(10,120,150,50);
		lblFilename.setBounds(10,180,150,50);

		txtConStr=new JTextField();
		txtUsername=new JTextField();
		txtPass=new JTextField();
		txtFilename=new JTextField();

		txtConStr.setBounds(180,10,150,50);
		txtUsername.setBounds(180,60,150,50);
		txtPass.setBounds(180,120,150,50);
		txtFilename.setBounds(180,180,150,50);

		btnConnect=new JButton("Connect");
		btnTest=new JButton("Test Connection");
		btnCancel=new JButton("Cancel");

		btnConnect.setBounds(10,250,100,50);
		btnTest.setBounds(150,250,100,50);
		btnCancel.setBounds(290,250,100,50);

		btnConnect.addActionListener(this);
		btnTest.addActionListener(this);
		btnCancel.addActionListener(this);


		Container c= getContentPane();
		setLayout(null);


		c.add(lblConStr);
		c.add(txtConStr);
		c.add(lblUsername);
		c.add(txtUsername);
		c.add(lblPass);
		c.add(txtPass);
		c.add(lblFilename);
		c.add(txtFilename);
		c.add(btnConnect);
		c.add(btnTest);
		c.add(btnCancel);

		setBounds(0,0,400,400);
		setVisible(true);

	}

	/*	public static void main(String args[])
	{
		new guiDatabase();

	}
	 */

	public void actionPerformed(ActionEvent ae)
	{
		JButton but=(JButton)ae.getSource();

		if(but==btnCancel)
		{
			JOptionPane.showMessageDialog(null,"Cancel");
			System.exit(0);
		}
		else 
			if(but==btnConnect)
			{
				String s1= txtConStr.getText();
				String s2= txtUsername.getText();
				String s3= txtPass.getText();
				String s4= txtFilename.getText();

//				DatabaseManager databaseManager = new DatabaseManager(s1,s2,s3);
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Cancel");
				System.exit(0);
			}

	}

}

